package cn.christ.com.mobilesafe.receiver;

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.telephony.SmsMessage;

import cn.christ.com.mobilesafe.R;
import cn.christ.com.mobilesafe.common.utils.LogUtils;
import cn.christ.com.mobilesafe.common.utils.SPUtils;
import cn.christ.com.mobilesafe.constant.Constants;
import cn.christ.com.mobilesafe.service.GPSService;

public class SmsReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		//
		DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
		ComponentName who = new ComponentName(context, MobileSafeAdminReceiver.class);
		// 获取短信内容，号码
		Object[] objs = (Object[]) intent.getExtras().get("pdus");
		for (Object obj : objs) {
			SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
			String body = smsMessage.getDisplayMessageBody();
			String phone = smsMessage.getOriginatingAddress();
			// 获取安全号码
			String safeNum = (String) SPUtils.get(Constants.SJFD_SAFE_NUM,"");
			if (safeNum.equals(phone)) {
				if ("#*music*#".equals(body)) {
					LogUtils.d("播放报警音乐");
					// MediaPlayer mp=new MediaPlayer();
					MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.alarm);
					// 设置左右声道最大声
					mediaPlayer.setVolume(1.0f, 1.0f);
					// 设置循环播放
					mediaPlayer.setLooping(true);
					mediaPlayer.start();
				} else if ("#*location*#".equals(body)) {
					LogUtils.d("获取位置");
					Intent service = new Intent(context, GPSService.class);
					context.startService(service);
				} else if ("#*wipeData*#".equals(body)) {
					LogUtils.d("清除数据");
					if (dpm.isAdminActive(who)) {
						dpm.wipeData(0);
					}
				} else if ("#*lockscreen*#".equals(body)) {
					LogUtils.d("锁屏");
					// 判断设备管理员是否激活，假如激活执行锁屏，重置密码
					// ComponentName:应用组件的标识：Activity，Service，BroadcastReceiver,ContentProvider
					if (dpm.isAdminActive(who)) {
						// 远程锁屏
						dpm.lockNow();
						// 重置密码
						dpm.resetPassword("888888", 0);
					}

				}


			}
		}
	}

}
